﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MapEditor.GUI
{
    class LoadTileButton : Button
    {
        //Declare instance variable
        public bool clicked = false;

        //Constructor
        public LoadTileButton(Texture2D texture, Vector2 position) : base(texture, position)
        {

        }

        public override void Update()
        {
            clicked = base.Clicked;
            base.Update();
        }

        public override void Effect()
        {
            Game1.state = State.FREEZE;

            Forms.NewTileSheetForm frmTileSheet = new Forms.NewTileSheetForm();
            frmTileSheet.ShowDialog();

            if (frmTileSheet.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                Game1.Front1TileHeight = frmTileSheet.tileHeight1;
                Game1.Front1TileWidth = frmTileSheet.tileWidth1;
                Game1.Front1tileSheetFileName = frmTileSheet.sheetFileName1;

                Game1.Front2TileHeight = frmTileSheet.tileHeight2;
                Game1.Front2TileWidth = frmTileSheet.tileWidth2;
                Game1.Front2tileSheetFileName = frmTileSheet.sheetFileName2;

                Game1.Front3TileHeight = frmTileSheet.tileHeight3;
                Game1.Front3TileWidth = frmTileSheet.tileWidth3;
                Game1.Front3tileSheetFileName = frmTileSheet.sheetFileName3;

                Game1.PlayerTileHeight = frmTileSheet.tileHeight4;
                Game1.PlayerTileWidth = frmTileSheet.tileWidth4;
                Game1.PlayertileSheetFileName = frmTileSheet.sheetFileName4;

                Game1.EnemyTileHeight = frmTileSheet.tileHeight5;
                Game1.EnemyTileWidth = frmTileSheet.tileWidth5;
                Game1.EnemytileSheetFileName = frmTileSheet.sheetFileName5;

                Game1.ScreenTileHeight = frmTileSheet.tileHeight6;
                Game1.ScreenTileWidth = frmTileSheet.tileWidth6;
                Game1.ScreentileSheetFileName = frmTileSheet.sheetFileName6;

                Game1.Back1TileHeight = frmTileSheet.tileHeight7;
                Game1.Back1TileWidth = frmTileSheet.tileWidth7;
                Game1.Back1tileSheetFileName = frmTileSheet.sheetFileName7;

                Game1.Back2TileHeight = frmTileSheet.tileHeight8;
                Game1.Back2TileWidth = frmTileSheet.tileWidth8;
                Game1.Back2tileSheetFileName = frmTileSheet.sheetFileName8;

                Game1.Back3TileHeight = frmTileSheet.tileHeight9;
                Game1.Back3TileWidth = frmTileSheet.tileWidth9;
                Game1.Back3tileSheetFileName = frmTileSheet.sheetFileName9;

                base.prevClicked = false;

                try
                {
                    TextureCreationParameters tcp = TextureCreationParameters.Default;
                    tcp.ColorKey = Microsoft.Xna.Framework.Graphics.Color.Magenta;
                    tcp.Format = SurfaceFormat.Rgba32;

                    Game1.Front1TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Front1tileSheetFileName, tcp);
                    Game1.Front2TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Front2tileSheetFileName, tcp);
                    Game1.Front3TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Front3tileSheetFileName, tcp);
                    Game1.PlayerTileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.PlayertileSheetFileName, tcp);
                    Game1.EnemyTileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.EnemytileSheetFileName, tcp);
                    Game1.ScreenTileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.ScreentileSheetFileName, tcp);
                    Game1.Back1TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Back1tileSheetFileName, tcp);
                    Game1.Back2TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Back2tileSheetFileName, tcp);
                    Game1.Back3TileSheet = Texture2D.FromFile(Game1.graphics.GraphicsDevice, Game1.Back3tileSheetFileName, tcp);
                    
                    Game1.map.LoadTileSet(Game1.Front1TileSheet, Game1.Front1TileWidth, Game1.Front1TileHeight);
                    Game1.map.LoadTileSet(Game1.Front2TileSheet, Game1.Front2TileWidth, Game1.Front2TileHeight);
                    Game1.map.LoadTileSet(Game1.Front3TileSheet, Game1.Front3TileWidth, Game1.Front3TileHeight);
                    Game1.map.LoadTileSet(Game1.PlayerTileSheet, Game1.PlayerTileWidth, Game1.PlayerTileHeight);
                    Game1.map.LoadTileSet(Game1.EnemyTileSheet, Game1.EnemyTileWidth, Game1.EnemyTileHeight);
                    Game1.map.LoadTileSet(Game1.ScreenTileSheet, Game1.ScreenTileWidth, Game1.ScreenTileHeight);
                    Game1.map.LoadTileSet(Game1.Back1TileSheet, Game1.Back1TileWidth, Game1.Back1TileHeight);
                    Game1.map.LoadTileSet(Game1.Back2TileSheet, Game1.Back2TileWidth, Game1.Back2TileHeight);
                    Game1.map.LoadTileSet(Game1.Back3TileSheet, Game1.Back3TileWidth, Game1.Back3TileHeight);
                }
                catch
                {
                    System.Windows.Forms.MessageBox.Show("There was an error loading the texture");
                }

            }
            else
            {
                base.prevClicked = false;
            }

            Game1.state = State.PLAY;

            base.Effect();
        }
    }
}
